var makeOrder;
$(function () {
    makeOrder = new Vue({
        el:'#makeOrderController',
        data:{
            auction:{},
            addressList:[],
            borders:[],
            address:{
                userName:'',
                area:'',
                address:'',
                phone:'',
                id:''
            },
            order:{
                address:{},
                payType:1,
                auctionId:_id,
                border:0,
                borderPrice:0,
                expressPrice:0,
                totalPrice:0,
                servicePrice:0,
                realServicePrice:0,
                creditRatio:'',
                price:0,
                cash:0,
                creditPrice:0
            },
            param:{
                showAddress : false,
                showPayType : false
            },
            vip:''
        },
        watch:{
            'order.border':function (n,o) {
                var that = this;
                if (n == 1){
                    that.order.totalPrice = that.order.price.add(that.auction.packingPay).add(that.auction.expressPay).add(that.order.realServicePrice).sub(that.order.cash);
                }else{
                    that.order.totalPrice = that.order.price.add(that.auction.expressPay).add(that.order.realServicePrice).sub(that.order.cash);
                }
            }
        },
        methods:{
            showAddress:function (o) {
                this.param.showAddress = o;
            },
            showPayType:function (o) {
                this.param.showPayType = o;
            },
            selectPayType:function (type) {
                this.order.payType = type;
            },
            addOrder:function () {
                var that = this;
                var param = {
                    type:_type,
                    id:that.order.auctionId,
                    addressId:that.order.address.id,
                    payType:that.order.payType,
                    borderId:that.order.border
                };
                if (!param.addressId){
                    sy.msg('请选择收货地址');
                    return;
                }
                sy.post('/web/auction/addOrder',param,function (data) {
                    if (data.errorCode){
                        sy.msg(data.errorMsg);
                        return;
                    }
                    if (that.order.payType == 1){
                        //支付宝
                        $("#content").val(data.pay);
                        $("#form").submit();
                    }else{
                        //微信二维码
                        var url = sy.path + '/qr?msg='+data.qr;
                        sy.showWxQrPay(data.price,url,function(){window.location.href = sy.path + "/web/home/order";},null);
                    }
                })
            },
            selectAddress:function (address) {
                this.order.address = address;
            },
            loadAuction:function () {
                var that = this;
                sy.post('/web/auction/detailById',{id:_id,type:_type},function (data) {
                    if (data.errorCode){
                        sy.msg(data.errorMsg,function () {
                            that.order.auctionId = '';
                            location.href = sy.path+"/web";
                        });
                        return;
                    }
                    var auction = data.data;
                    that.auction = auction;
                    that.order.totalPrice = data.price.add(auction.expressPay).add(data.realServicePrice);
                    that.order.borderPrice = auction.packingPay;
                    that.order.expressPrice = auction.expressPay;
                    that.order.servicePrice = data.servicePrice;
                    that.order.creditRatio = data.creditRatio;
                    that.order.realServicePrice = data.realServicePrice;
                    that.order.price = data.price;
                    that.order.creditPrice = data.creditPrice;
                    if (data.cash){
                        that.order.cash = data.cash;
                    }
                    that.vip = data.vip;
                })
            },
            loadAddress:function (address) {
                var that = this;
                sy.postApi('/api/userInfo/myAddressList',{},function (data) {
                    that.addressList = data;
                    var has = false;
                    if (address){
                        $.each(data,function(i,o){
                            if (o.area == address.area && o.address == address.address && o.phone == address.phone && o.name == address.userName){
                                that.order.address = o;
                                has = true;
                            }
                        });
                    }
                    if (!has && that.order.address){
                        $.each(data,function(i,o){
                            if (that.order.address && that.order.address.id == o.id){
                                that.order.address = o;
                                has = true;
                            }
                        });
                    }
                    if (!has){
                        $.each(data,function(i,o){
                            if (o.is_default == 1){
                                that.order.address = o;
                            }
                        });
                    }
                })
            },
            addAddress:function () {
                var that = this;
                var url = '/api/userInfo/addMyAddress';
                if (that.address.id){
                    url = '/api/userInfo/updateMyAddress';
                }
                sy.postApi(url,that.address,function (data) {
                    that.showAddress(false);
                    that.loadAddress(that.address);
                    that.address = {
                        userName:'',
                        area:'',
                        address:'',
                        phone:'',
                        id:''
                    };
                })
            },
            editAddress:function (item) {
                var that = this;
                this.address = item;
                this.address.userName = item.name;
                this.address.type = 1;
                that.showAddress(true);
            }
        },
        created:function () {
            var that = this;
            that.loadAddress();
            that.loadAuction();
        }
    });
})